﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BawangMemo.BackendModels
{
    public class BookRepository
    {
        public static List<BookInfo> GetBookList()
        {
            List<BookInfo> result = new List<BookInfo>();
            var fileList = Directory.GetFiles(Constants.BookDirectory);
            foreach (string path in fileList)
            {
                if (!path.EndsWith(".xml"))
                {
                    continue;
                }
                int startIndex = path.LastIndexOf('/') + 1;
                int endIndex = path.LastIndexOf('.');
                string bookName = path.Substring(startIndex, endIndex - startIndex);
                result.Add(new BookInfo
                {
                    BookName = bookName,
                    BookPath = path,
                    TagCategoryPath = Path.GetDirectoryName(path) + @"\predefined_tags\" + bookName + ".cat",
                    TagDataPath = Path.GetDirectoryName(path) + @"\predefined_tags\" + bookName + ".tag",
                });
            }
            return result;
        }

        public static BookInfo GetBookInfo(string bookName)
        {
            return GetBookList().Where(book => book.BookName.Equals(bookName)).First();
        }
    }
}
